package com.xxx.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.xxx.common.dto.Pagination;
import com.xxx.common.utils.DateUtil;
import com.xxx.model.SysUserRole;

@Repository
public class SysUserRoleDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 保存
	 * @param sysUserRole
	 * @return id
	 */
	public long save(SysUserRole sysUserRole){
		String sql = "insert into sys_user_role(id,roleid,userid,recordstatus,createtime,updatetime) values (?,?,?,1,?,?)";
		Object obj[] = {sysUserRole.getId(),sysUserRole.getRoleid(),sysUserRole.getUserid(),sysUserRole.getCreatetime(),sysUserRole.getUpdatetime()};
		int ret = jdbcTemplate.update(sql, obj);
		if (ret > 0) {
			return sysUserRole.getId();
		}
		return 0;
	}
	
	/**
	 * 修改
	 * @param sysUserRole
	 * @return id
	 */
	public long update(SysUserRole sysUserRole){
		String sql = "update sys_user_role set roleid=?,userid=?,updatetime=? where id=?";
		Object obj[] = {sysUserRole.getRoleid(),sysUserRole.getUserid(),sysUserRole.getUpdatetime(), sysUserRole.getId()};
		int ret = jdbcTemplate.update(sql, obj);
		if (ret > 0) {
			return sysUserRole.getId();
		}
		return 0;
	}
	
	/**
	 * 逻辑删除
	 * @param id
	 * @return true/false
	 */
	public boolean delete(long id){
		String sql = "update sys_user_role set recordstatus=0,updatetime=? where id=?";
		Object obj[] = {DateUtil.dateToLong(),id};
		int ret = jdbcTemplate.update(sql, obj);
		return ret > 0;
	}
	
	/**
	 * 获取一个
	 * @param id
	 * @return SysUserRole
	 */	
	public SysUserRole getEntity(long id){
		String sql = "select id,roleid,userid,createtime,updatetime from sys_user_role where recordstatus=1 and id=?";
		final SysUserRole sysUserRole = new SysUserRole();
		jdbcTemplate.query(sql, new Object[]{id},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				getResultSet(rs, sysUserRole);
            }
		});
		return sysUserRole;
	}

	/**
	 * 获取list
	 * @param whereMap
	 * @return list
	 */		
	public List<SysUserRole> getList(Map<String, Object> whereMap){
		final List<SysUserRole> list = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,roleid,userid,createtime,updatetime from sys_user_role where recordstatus=1 ");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		jdbcTemplate.query(sql.toString(), obj ,new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				list.add(getResultSet(rs, new SysUserRole()));
            }
		});
		return list;
	}

	/**
	 * 获取list,分页
	 * @param whereMap
	 * @param pagination
	 * @return list
	 */			
	public List<SysUserRole> getList(Map<String, Object> whereMap,Pagination pagination){
		final List<SysUserRole> list = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,roleid,userid,createtime,updatetime from sys_user_role where recordstatus=1 ");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		sql.append(" order by " + pagination.getOrder());
		sql.append(" limit " + pagination.getPage() + "," + pagination.getPageSize());
		jdbcTemplate.query(sql.toString(), obj ,new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				list.add(getResultSet(rs, new SysUserRole()));
			}
		});
		return list;
	}

	/**
	 * 获取个数
	 * @param whereMap
	 * @return int
	 */		
	public int getCount(Map<String, Object> whereMap){
		StringBuffer sql = new StringBuffer("select count(1) from sys_user_role where recordstatus=1");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		Integer count = jdbcTemplate.queryForObject(sql.toString(), obj, Integer.class);
		if(count != null){
			return count;
		}
		return 0;
	}
	
	/**
	 * 获取条件
	 * @param whereMap
	 * @param sql
	 * @param whereObj
	 * @return sql
	 */	
	private StringBuffer getCondition(Map<String, Object> whereMap,StringBuffer sql,List<Object> whereObj){
		if(whereMap == null){
			return sql;
		}
		if (whereMap.containsKey("roleid")) {
			long roleid = (long) whereMap.get("roleid");
			sql.append(" and roleid=?");
			whereObj.add(roleid);
		}
		if (whereMap.containsKey("userid")) {
			long userid = (long) whereMap.get("userid");
			sql.append(" and userid=?");
			whereObj.add(userid);
		}
		return sql;
	}
	
	/**
	 * 获取结果集
	 * @param whereMap
	 * @param sysUserRole
	 * @return sysUserRole
	 */	
	private SysUserRole getResultSet(ResultSet rs,SysUserRole sysUserRole) throws SQLException{
		sysUserRole.setId(rs.getLong("id"));
		sysUserRole.setRoleid(rs.getLong("roleid"));
		sysUserRole.setUserid(rs.getLong("userid"));
		sysUserRole.setCreatetime(rs.getLong("createtime"));
		sysUserRole.setUpdatetime(rs.getLong("updatetime"));
		return sysUserRole;
	}
}